#include "point.h"
#include <QDebug>
Point::Point(const QPointF &pos, QGraphicsItem *parent, QGraphicsScene *scene, int d) :
    QGraphicsEllipseItem(pos.x()-d/2, pos.y()-d/2, d, d, parent, scene), state(commons::DEFAULT),
    /*onDrag(false),*/ diameter(d)
{
    this->setEnabled(true);
    this->setBrush(Qt::black);
    this->setAcceptHoverEvents(true);
    circle = new QGraphicsEllipseItem(pos.x()-d, pos.y()-d, 2*d, 2*d, this, scene);
    circle->setVisible(false);
//    qDebug()<<"point pos: "<<pos;
//    circle->setBrush(Qt::black);
}

Point::Point(const Point &p) :
    QGraphicsEllipseItem(p.rect(), p.parentItem(), p.scene()), state(p.state),
  diameter(p.diameter)
{
    qDebug() << "point construct" ;
    this->setEnabled(p.isEnabled());
    this->setBrush(p.brush());
    this->setAcceptHoverEvents(true);
    this->circle = new QGraphicsEllipseItem(p.circle->rect(), this, scene());
    this->circle->setVisible(p.circle->isVisible());
}

Point::~Point()
{
    scene()->removeItem(circle);
    scene()->removeItem(this);
    delete circle;
}

void Point::moveTo(const QPointF &dest)
{
    this->setRect(dest.x()-diameter/2, dest.y()-diameter/2, diameter, diameter);
    circle->setRect(dest.x()-diameter, dest.y()-diameter, 2*diameter, 2*diameter);
//    emit pointMoved(this);
}

void Point::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    qDebug() << "enter point " << event->pos();
    circle->setVisible(true);
    this->parentItem()->update();
    emit hoverEntered();
}

void Point::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    circle->setVisible(false);
    this->parentItem()->update();
    emit hoverLeft();
}

void Point::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{

//    qDebug() << "Point: " << state;
    if (state == commons::POINT_MOVING) {
         qDebug()<< "Point: " << event->pos();
        this->setRect(event->pos().x()-diameter/2, event->pos().y()-diameter/2, diameter, diameter);
        circle->setRect(event->pos().x()-diameter, event->pos().y()-diameter, 2*diameter, 2*diameter);
        emit pointMoved(this);
        this->parentItem()->update();
    }

    // Ignore this event so that Point's parentItem Canvas can receive it.
//    event->ignore();
}

void Point::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    // Ignore press down event on right button.
    if (event->button() == Qt::RightButton)
        return;

    if (state == commons::POINT || state == commons::PLAYING) {
        prevState = state;
        state = commons::POINT_MOVING;
        emit dragStart();
    } else if (state == commons::LINE) {
        qDebug() << "point liner";
        // Ignore this event so that Point's parentItem Canvas can receive it.
        event->ignore();
        emit linerStart(this);
    } else {}

}

void Point::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    if (state == commons::POINT_MOVING) {
        state = prevState;
        emit dragStop();
    } else if (state == commons::POINT) {
        qDebug() << state;
        if (event->button() == Qt::RightButton) {
            qDebug() << "right click on point";
            emit deletePoint(this);
        }
    }

    // Ignore this event so that Point's parentItem Canvas can receive it.
//    event->ignore();

}
